package com.videoweb.controller;


import com.videoweb.annotation.Anonymous;
import com.videoweb.entity.Favorites;
import com.videoweb.entity.VideoFavorite;
import com.videoweb.entity.dto.CheckFavoritesDTO;
import com.videoweb.service.IFavoritesService;
import com.videoweb.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lmr
 * @since 2025-05-17
 */


@RestController
@RequestMapping("/favorites")
public class FavoritesController {

    @Autowired
    IFavoritesService favoritesService;


//   查询收藏夹
    @GetMapping("/{id}")
    public AjaxResult getAllFavorites(@PathVariable("id") Long userId) {
        AjaxResult result = AjaxResult.success();
        result.put("favorites", favoritesService.getAllFavoritesByUserId(userId));
//        根据收藏夹来得到视频呀
        return result;
    }


//    添加收藏夹
    @PostMapping("/createFavorites")
    public AjaxResult addFavorites(@RequestBody Favorites favorites){
        AjaxResult result;
        if (favoritesService.createFavorites(favorites)){
            result = AjaxResult.success("创建收藏夹成功！");
        }else {
            result = AjaxResult.error("创建收藏夹失败！");
        }
        return result;
    }

//    删除收藏夹
    @DeleteMapping("/removeFavorites/{id}")
    public AjaxResult removeFavorites(@PathVariable("id") Long Id){
        AjaxResult result =AjaxResult.success();

        if (favoritesService.removeFavoritesByFavorId(Id)){
            result = AjaxResult.success("删除收藏夹成功！");
        }else {
            result = AjaxResult.error("删除收藏夹失败！");
        }
        return result;

    }


//   对收藏夹的进行添加和删除

    @PostMapping("/addVideoToFavorites")
    public AjaxResult addVideoToFavorites(@RequestBody VideoFavorite videoFavorite) {

        AjaxResult result;
        if (favoritesService.addVideoFavorites(videoFavorite)) {
            result = AjaxResult.success("添加视频成功！");
        } else {
            result = AjaxResult.error("添加视频失败！");
        }
        return result;
    }

    @PostMapping("/addToMultiple")
    public AjaxResult addVideoToMultipleFavorites(@RequestBody Map<String, Object> requestData) {
        try {
            Long videoId = (Long) requestData.get("videoId");
            @SuppressWarnings("unchecked")
            List<Long> favoritesIds = (List<Long>) requestData.get("favoritesIds");

            if (videoId == null || favoritesIds == null || favoritesIds.isEmpty()) {
                return AjaxResult.error("参数不完整");
            }
            // 批量保存视频收藏中间表
            favoritesService.saveBatchFavorites(videoId, favoritesIds);

            return AjaxResult.success("批量添加成功");

        } catch (Exception e) {
            return AjaxResult.error("批量添加失败：" + e.getMessage());
        }
    }





    @DeleteMapping("/cancelVideoFromFavorites/{videoId}/{favoritesId}")
    public AjaxResult cancelVideoFromFavorites(@PathVariable("videoId") Long videoId, @PathVariable("favoritesId") Long favoritesId) {
        AjaxResult result;

        if (favoritesService.cancelVideoFromFavorites(videoId,favoritesId)) {
            result = AjaxResult.success("取消收藏成功！");
        } else {
            result = AjaxResult.error("取消收藏失败！");
        }
        return result;
    }




@Anonymous
    @GetMapping("/checkStatus/{videoId}")
    public AjaxResult checkFavorites(@PathVariable("videoId")Long videoId) {
        AjaxResult result =AjaxResult.success();


        CheckFavoritesDTO dto = favoritesService.checkVideoFavorites(videoId);
        result.put("data", dto);
        return result;
    }




}
